package com.bj58.groupbuy.action.service.impl;

import java.util.List;

import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.db.UserPriviledgeDao;
import com.bj58.groupbuy.action.model.Role;
import com.bj58.groupbuy.action.service.RoleManagerService;
import com.bj58.groupbuy.service.impl.BaseService;
import com.bj58.groupbuy.wf.page.Paginate;

public class RoleManagerServiceImpl extends BaseService<Role> implements RoleManagerService {
	private static UserPriviledgeDao comDao = new UserPriviledgeDao();
	@Override
	public Role getRoleByName(String roleName) throws Exception {
		String sql = "select * from role where rolename='"+roleName+"'";
		List<Role> roleList=comDao.getAllColsByVo(sql, Role.class, DBConfig.db_68_bi_sys.getIntType());
		if(null != roleList && !roleList.isEmpty() ){
			return roleList.get(0);
		}
		return null;
	}

	@Override
	public List<Role> getAllRoles() throws Exception {
		String sql = "select * from role where state = 1";
		List<Role> roleList=comDao.getAllColsByVo(sql, Role.class, DBConfig.db_68_bi_sys.getIntType());
		if(null != roleList &&  !roleList.isEmpty()){
			return roleList;
		}
		return null;
	}

	@Override
	public Paginate<Role> getRoleByPage(int pageIndex, int pageSize,
			String where) throws Exception {
		return getGenericDao().findForPage(Role.class, where, "*", pageIndex, pageSize, "createtime desc");
	}

	@Override
	public Role getRoleById(int roleid) throws Exception {
		String sql = "select * from role where id = " + roleid;
		List<Role> roleList=comDao.getAllColsByVo(sql, Role.class, DBConfig.db_68_bi_sys.getIntType());
		if(roleList.size()>0)
			return roleList.get(0);
		else 
			return null;
	}

	@Override
	public boolean saveRole(Role r) throws Exception {
		int res=comDao.insertOneByVo(null, "role", Role.class, r, DBConfig.db_68_bi_sys.getIntType());
		return res==0?false:true;
	}

	@Override
	public boolean updateRole(Role role) throws Exception {
		return comDao.update(null, "role", Role.class, role, "id="+role.getId(),  DBConfig.db_68_bi_sys.getIntType());
	}


	
}
